import Code from "components/Markdown/Code"

export const meta = {
  title: "Prisma & Sequelize",
  position: 70,
  description: "Learn how Prisma compares to Sequelize"
}

## Reading data

### Fetching single objects

Sequelize

```ts
const user = await User.findByPk(id)
```

Prisma

```ts
const user = await prisma.user({ id })
```

### Fetching selected scalars of single objects

Sequelize

```ts
const user = await User.findByPk(id, attributes: {
  ["name", "email"]
})
```

Prisma

```ts
const userFragment = await prisma.user({ id }).$fragment(`
  fragment NameAndEmail on User { id email }`
`)
```

### Fetching relations

Sequelize

```ts
const user = await User.findByPk(id, {
  include: [{
    model: Post
  }]
})
```

Prisma

<Code languages={["Fluent API", "Using fragments", "Native GraphQL"]}>

```ts
const postsByUser = await prisma
  .user({ id })
  .posts()
```

```ts
const userWithPosts = await prisma
  .user({ id })
  .$fragment(`
    fragment UserPosts on User {
      posts { id title content published }
    }
  `)
```

```ts
const userWithPosts = await prisma
  .$graphql(`
    query ($id: ID!) {
      user(id: $id) {
        posts { id title content published }
      }
    }
  `, 
  { id }
  )
```

</Code>

### Filtering for concrete values

Sequelize

```ts
const user = User.findAll({
  where: {
    name: "Alice"
  }
})
```

Prisma

```ts
const user = await prisma.users({
  where: {
    name: "Alice"
  }
})
```

### Other filter criteria

Sequelize

Sequelize has an extensive set of operators to be found [here](http://docs.sequelizejs.com/manual/tutorial/querying.html#operators).

Prisma

Prisma generates many additional filters that are commonly used in modern application development:

- `<field>_ends_with` & `<field>_starts_with`
- `<field>_not_ends_with` & `<field>_not_starts_with`
- `<field>_gt` & `<field>_gte`
- `<field>_lt` & `<field>_lte`
- `<field>_contains` & `<field>_not_contains`
- `<field>_in` & `<field>_not_in`

### Relation filters

Sequelize

Sequelize doesn't offer a dedicated API for relation filters. You can get similar functionality by sending a raw SQL query to the database.

Prisma

Prisma lets you filter a list based on a criteria that applies not only to the models of the list being retrieved, but to a _relation_ of that model. 

For example, you want to fetch only those users that wrote a post with the title "Hello World". The filter criteria is therefore not referencing the user model, but the post model that's related to the user model:

```ts
query {
  user(where: {
    posts_some: {
      title: "Hello World"
    }
  }) {
    id
  }
}
```

### Pagination

Sequelize

```ts
const posts = await Post.findAll({ 
  offset: 5, 
  limit: 10 
})
```

Prisma

```ts
const posts = await prisma.posts({
  skip: 5,
  first: 10
})
```

In addition to `skip` and `first`, the Prisma API also offers:

- `last`
- `before` & `after` for cursor based pagination
- Relay-style pagination

## Writing data

### Creating objects

Sequelize

<Code languages={["Using `save`", "Using `create`"]}>

```ts
const user = await User.build({
  name: "Alice",
  email: "alice@prisma,io"
})
await user.save()
```

```ts
const user = await User.create({
  name: "Alice",
  email: "alice@prisma,io"
})
```

</Code>

Prisma

```ts
const user = await new User({
  name: "Alice",
  email: "alice@prisma.io"
})
```

### Updating objects

Sequelize

<Code languages={["Using `save`", "Using `update`"]}>

```ts
user.name = "James"
user.email =" alice@prisma.com"
await user.save()
```

```ts
await user.update({
  name: "James",
  email: "james@prisma.io"
})
```

</Code>

Prisma

```ts
const updatedUser = await prisma.updateUser({
  where: { id },
  data: {
    name: "James",
    email: "james@prisma.io"
  }
})
```

### Deleting objects

Sequelize

```ts
await user.destroy()
```

Prisma

```ts
const deletedUser = await prisma.deleteUser({ id })
```

### Batch updates

Sequelize

```ts
const updatedUsers = await User.update({ 
  { role: "Admin" },
  where: {
    email: {
      [Op.like]: "%@prisma.io"
    }
  },
})
```

Prisma

```ts
const updatedUsers = await prisma
  .updateManyUsers({
    data: { role: "ADMIN" },
    where: {
      email_ends_with: "@prisma.io"
    }
  })
```

### Batch deletes

Sequelize

```ts
await User.destroy({
  where: {
    id: {
      [Op.in]: [id1, id2, id3]
    }
  }
})
```

Prisma

```ts
await prisma.deleteManyUsers({
  id_in: [id1, id2, id3]
})
```

### Transactions

Sequelize

<Code languages={["Manual", "Automatic"]}>

```ts
return sequelize.transaction(async t => {
  const user = await User.create({
    name: "Alice",
    email: "alice@prisma,io"
  }, { 
    transaction: t 
  })
  const post1 = await Post.create({
    title: "Join us for GraphQL Conf in 2019"
  }, { 
    transaction: t 
  })
  const post2 = await Post.create({
    title: "Subscribe to GraphQL Weekly for GraphQL news"
  }, { 
    transaction: t 
  })
  await user.setPosts([post1, post2])
})
```

```ts
return sequelize.transaction(async transaction => {
  try {
      const user = await User.create({
      name: "Alice",
      email: "alice@prisma,io"
    })
    const post1 = await Post.create({
      title: "Join us for GraphQL Conf in 2019"
    })
    const post2 = await Post.create({
      title: "Subscribe to GraphQL Weekly for GraphQL news"
    })
    await user.setPosts([post1, post2])
  } catch(e) {
    return t.rollback()
  }
})
```

</Code>

Prisma

```ts
const newUser: User = await prisma.createUser({
  name: "Bob",
  email: "bob@prisma.io",
  posts: {
    create: [
      {
        title: "Join us for GraphQL Conf in 2018"
      },
      {
        title: "Subscribe to GraphQL Weekly for GraphQL news"
      }
    ]
  }
})
```
